<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<button onclick="encryption()">加密提交</button>
<button onclick="decryption()">解密数据</button>
<script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.5.1/jquery.js"></script>
<script src="https://cdn.bootcdn.net/ajax/libs/jsencrypt/3.0.0-rc.1/jsencrypt.js"></script>
<script>

    const PUBLIC_KEY = 'MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAn0MVxeKJM36xHVlivHBMfr5hY+UwLGGJ6H32zV5rruX38grA46CeZWncTEKq2QYrrvsyN6FU+gQeA9vXyeXypeAe8Hmv2dfVo2o+5vLO4E13KBoqT6iADEmfPkA/fkg32r/qzxK/DzMUBkkil8cqqH5+QSsLpvTB/F4FalNoB6k7Fov83Pqlc19f6nXDfWoFr4+fAONcwnBzSMr2JhLNIZsM2WPmu3JdxaVUX9a6lpSrlsFU1uDszWmLjI8nwKdudISqqGLDyaATlm88z4xaqVPlbPNeG4Ht+TNNRHGFXfD6XlTb9O9AhQ2S6Rmc/N+lg3pT8oJ9z9KndkdkuphHfQIDAQAB';

    const PRIVATE_KEY = 'MIIEvAIBADANBgkqhkiG9w0BAQEFAASCBKYwggSiAgEAAoIBAQCfQxXF4okzfrEdWWK8cEx+vmFj5TAsYYnoffbNXmuu5ffyCsDjoJ5ladxMQqrZBiuu+zI3oVT6BB4D29fJ5fKl4B7wea/Z19Wjaj7m8s7gTXcoGipPqIAMSZ8+QD9+SDfav+rPEr8PMxQGSSKXxyqofn5BKwum9MH8XgVqU2gHqTsWi/zc+qVzX1/qdcN9agWvj58A41zCcHNIyvYmEs0hmwzZY+a7cl3FpVRf1rqWlKuWwVTW4OzNaYuMjyfAp250hKqoYsPJoBOWbzzPjFqpU+Vs814bge35M01EcYVd8PpeVNv070CFDZLpGZz836WDelPygn3P0qd2R2S6mEd9AgMBAAECggEABtuWpmy4x7Te34qny8FyKcr82pZV2rNggthZtkIolOFmKc/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D8j+xQP4AjU8SMZZfwnj9Sr5Vc6AFFZHYHr6n9VfSF+puoDcmaU6MLqcOI9qpJmHKe5F2+HhgDlVGDVH60BrjQwGUnw==';

    /**
     * 加密提交
     */
    function encryption() {
        var data = {
            username: "admin",
            password: "123456",
            nickname: "超级管理员",
            sex: "男",
            age: "29",
            telephone: "13111111111",
            address: "北京市西城区"
        };
        // 加密
        var encrypt = new JSEncrypt();
        encrypt.setPublicKey(PUBLIC_KEY);
        var encrypted = encrypt.encrypt(JSON.stringify(data));
        console.log('加密前数据:%o', data);
        console.log('加密后数据:%o', encrypted);

        $.ajax({
            url: "/test/decryption",
            type: "POST",
            contentType: "application/json;charset=utf-8",
            data: encrypted,
            success: function (data) {
                console.log(JSON.stringify(data))
            }
        })
    }

    /**
     * 解密数据
     */
    function decryption() {
        $.ajax({
            url: "/test/encryption",
            type: "GET",
            contentType: "application/json;charset=utf-8",
            success: function (data) {
                var decrypt = new JSEncrypt();
                decrypt.setPrivateKey(PRIVATE_KEY);
                var relData = decrypt.decrypt(data);
                console.log('加密前数据:%o', data);
                console.log('加密后数据:%o', relData);
            }
        })
    }
</script>
</body>
</html>